package gmu.xytxl.servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import gmu.xytxl.util.PlugUtils;

/**
 * 用于处理在使用webuploader插件中删除文件的操作
 */
@WebServlet("/DelFileLayUI")
public class DelFileLayUI extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DelFileLayUI() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(request.getParameter("imgpath"));
		
		String serverPath = request.getSession().getServletContext().getRealPath("/");
		System.out.println(serverPath);
		String filePath = serverPath+request.getParameter("imgpath").replace("http://127.0.0.1:8080/XYTXL/", "");
		
	//	String filePath = request.getParameter("imgpath");
		System.out.println(filePath);
		File file = new File(filePath);
		if (file.delete()) {
			JSONObject json = new JSONObject();
			json.put("code", 0);// 引用路径
			json.put("msg", "删除成功");
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json; charset=UTF-8");
            response.getWriter().write(json.toString());  
    		response.getWriter().flush();
    		System.out.println( "删除成功");
		}
		
		
	}

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
